package com.jzo2o.health.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jzo2o.health.model.excel.ReservationImportData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * EasyExcel工具类
 *
 * @Author yzx
 * @Date 2024/12/14 15:59
 */
@Slf4j
public class EasyExcelUtils {

    public static List<ReservationImportData> getContentOfExcel(MultipartFile file){
        try {
            InputStream inputStream = file.getInputStream();
            return getContentOfExcel(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static List<ReservationImportData> getContentOfExcel(InputStream inputStream) {
        List<ReservationImportData> list = new ArrayList<>();
        EasyExcel.read(inputStream, ReservationImportData.class, new AnalysisEventListener<ReservationImportData>() {

            @Override
            public void invoke(ReservationImportData reservationImportData, AnalysisContext analysisContext) {
                list.add(reservationImportData);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                log.info("读取完成");
            }
        }).sheet().doRead();
        return list;
    }
}
